//*****************************************************************************
//
// Lab7.c - user programs
// Jonathan Valvano, Feb 4, 2011, EE345M
// Alex Santos, Matt Villarrubia
//*****************************************************************************
// feel free to adjust these includes as needed
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "rit128x96x4.h"
#include "adc_c.h"
#include "os.h"
#include "fifo.h"
#include "edisk.h"
#include "efile.h"
#include "ping.h"
#include "motor.h"
#include "LM3SXXXX.H"
#include "IR.h"

_tFIFO Fifo;

Sema4Type oLED;

#define GPIO_PF0  (*((volatile unsigned long *)0x40025004))
#define GPIO_PF1  (*((volatile unsigned long *)0x40025008))
#define GPIO_PF2  (*((volatile unsigned long *)0x40025010))
#define GPIO_PF3  (*((volatile unsigned long *)0x40025020))
#define GPIO_PG1  (*((volatile unsigned long *)0x40026008))

void UARTSend(const unsigned char *pucBuffer);

void On(OS_TCB* this){
	
	for(;;){
		GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0x80); //generate pulse
		OS_Sleep(this,7);
		GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0x00); //generate pulse
		OS_Sleep(this,7);	
	}
}

void Forever(OS_TCB* this){
	for(;;);
}


//void Ping(OS_TCB *this){
//	int distance;
//	char buf[30];
//	for(;;){
//		distance = Ping_Measure(this);
//		
//		if(distance == -1)
//			continue;
//		else
//			sprintf(buf,"Dist (in) %d        ",(int)distance);
//		
//		oLED_Message(1,3,buf);
//	}
//}



void
InitConsole(void)
{
    //
    // Enable GPIO port A which is used for UART0 pins.
    // TODO: change this to whichever GPIO port you are using.
    //
	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Configure the pin muxing for UART0 functions on port A0 and A1.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);

    //
    // Select the alternate (UART) function for these pins.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

	 //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

	//
    // Enable the UART interrupt.
    //
    IntEnable(INT_UART0);
    UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

//    UARTStdioInit(0);
}

void TimeOut(OS_TCB *this){
	OS_Sleep(this,180000);
	IntMasterDisable();
	Motor_Set(MOTOR_LEFT,1);
	Motor_Set(MOTOR_RIGHT,1);
	for(;;);
}

//*******************final user  DEMONTRATE THIS TO TA**********
int main(void){

	OS_Init();           // initialize, disable interrupts
  

	//********initialize communication channels
  	//OS_MailBox_Init();
  	//Fifo = FIFO_CreateFifo(32, sizeof(char));

	//*******attach background tasks***********

  	//OS_AddThread(Ping,100,NORMAL_PRIORITY);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
	GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_7);
		
	OS_AddThread(IRDataCollect,100,HIGHEST_PRIORITY);
	
	OS_AddThread(MotorTask,100,HIGHEST_PRIORITY);
	
	OS_AddThread(Forever,100,LOW_PRIORITY);

	OS_AddThread(TimeOut,100,HIGHEST_PRIORITY);
	//OS_AddThread(On,100,LOW_PRIORITY);
	OS_Launch(1);
	
  
//  return 0;             // this never executes
}
